package cn.shyouchi.wuguanjia.android.ui.event.post;

import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.event.EventApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class AddPostActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.edit_post_name)
    EditText mEditPostName;
    @BindView(R.id.edit_post_content)
    EditText mEditPostContent;

    String projectId;

    EventApi mEventApi;

    PostEntity.ResultMessageBean entity;

    boolean isEdit;

    @Override
    public int initContentView() {
        return R.layout.activity_add_post;
    }

    @Override
    public void initInjector() {
        projectId = getIntent().getStringExtra("projectId");
        mEventApi = new EventApi(mContext);
        entity = getIntent().getParcelableExtra("post");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        mTopBar.setTitle(getTitle().toString());
        setSupportActionBar(mTopBar.getToolbar());

        if (entity != null) {
            initEntity();
            isEdit = true;
            ToastUtil.showToast("编辑状态");
        }

    }


    private void initEntity() {

        mEditPostName.setText(entity.getPost());
        mEditPostContent.setText(entity.getJobassignment());
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem = menu.add("保存");
        menuItem.setShowAsAction(2);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getTitle().equals("保存")) {
            postData();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void postData() {
        if (TextUtils.isEmpty(mEditPostName.getText())) {
            mEditPostName.setError("标题不能为空");
            return;
        }
        if (TextUtils.isEmpty(mEditPostContent.getText())) {
            mEditPostContent.setError("内容不能为空");
            return;
        }


        if (isEdit) {
            mEventApi.upPost(projectId,
                    String.valueOf(entity.getId()),
                    mEditPostName.getText().toString(),
                    mEditPostContent.getText().toString())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                        @Override
                        public void onNext(BaseRequestResult result) {
                            EventBus.getDefault().post(new DataChangeEvent());
                            finish();
                        }
                    });
        } else {
            mEventApi.addPost(projectId,
                    mEditPostName.getText().toString(),
                    mEditPostContent.getText().toString())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                        @Override
                        public void onNext(BaseRequestResult result) {
                            setResult(EventPostActivity.REQUEST_TYPE_ADD_SUCCESS);
                            finish();
                        }
                    });
        }
    }

}
